<?php

function cusform_user_register_form($form,&$form_state){
   
    $form['name'] = array(
        '#theme_wrappers' => array('custom_form_element'),
        '#type' => 'textfield',
        '#title' => t('User name'),
        '#default_value' => '',
        '#m_span' => 'input-type-6',
        '#attributes' => array(
        	'class' => array('ui-form input-text'),
        ),
     );
     
   $form['password'] = array(
        '#theme_wrappers' => array('custom_form_element'),
        '#type' => 'password',
        '#title' => t('Password'),
        '#m_span' => 'input-type-5',
        '#attributes' => array(
        	'class' => array('ui-form input-text')
        ),
    
    );
   
    $form['confirm_password'] = array(
        '#theme_wrappers' => array('custom_form_element'),
        '#type' => 'password',
        '#title' => t('Confirm password'),
        '#m_span' => 'input-type-5',
        '#attributes' => array(
        	'class' => array('ui-form input-text')
        ),
        
    );
    $form['full_name'] = array(
        '#theme_wrappers' => array('custom_form_element'),
        '#type' => 'textfield',
        '#title' => t('Full name'),
        '#m_span' => 'input-type-7',
        '#default_value' => '',
        '#required' => TRUE,
        '#attributes' => array(
        	'class' => array('ui-form input-text')
        ),
        //'#theme_wrappers' => array('m_wrapper_form_element_user')
    );
    $form['mail'] = array(
        '#theme_wrappers' => array('custom_form_element'),
        '#type' => 'textfield',
        '#title' => t('Email'),
        '#default_value' => '' ,
        '#m_span' => 'input-type-8',
        '#attributes' => array(
        	'class' => array('ui-form input-text'),
        	//'disabled' => true
        ),
        //'#theme_wrappers' => array('m_wrapper_form_element_user')
    );
    
    $form['gender'] = array(
        //'#theme_wrappers' => array('custom_form_element'),
        '#type' => 'radios',
        '#title' => t('Gender'),
        '#default_value' => '',
        '#options' => array(
        	1 => t('Male') , 
        	2 => t('Female')
        ),
        '#required' => TRUE,       
    );
    $form ['submit'] = array(
       // '#theme_wrappers' => array('custom_form_element'),
        '#type' => 'submit', 
        '#value' => t('Ok'),
        '#submit' => array('cusform_user_register_form_submit'),
    );
    $form['#theme_wrappers'] = array('custom_form');
    return $form;
}
function cusform_user_register_form_validate($form,&$form_state){
     
    //print_r($form_state['values']);die;
    if (isset($form_state['values']['password']) && $form_state['values']['password'] != $form_state['values']['confirm_password']) {
        form_set_error('confirm_password', t('password doesn\'t match'));
    }  
    
    if (isset($form_state['values']['name'])) {
       //Verify the syntax of the given name.
      $error = user_validate_name($form_state['values']['name']);
      if ($error) {
         form_set_error('name', $error);
      }
      
      //check username exist
      //$rsCheckName = db_query("SELECT COUNT(*) FROM {users} WHERE name = :name", array(':name' => $name))->fetchField();
      //$rsCheckName = db_select('users')->fields('users', array('uid'))->condition('name', db_like($form_state['values']['name']), 'LIKE')->range(0, 1)->execute()->fetchField();           
      $rsCheckName = user_load_by_name($form_state['values']['name']);
      if ($rsCheckName) {
          form_set_error('name', t('The name %name is already taken.', array('%name' => $form_state['values']['name'])));
      }
      
    }
    
    //check mail exist
    $errorEmail = user_validate_mail($form_state['values']['mail']);
    if ($errorEmail) {
	form_set_error('mail', $error);
    } 
    //(bool) db_select('users')->fields('users', array('uid'))->condition('uid', $account->uid, '<>')->condition('mail', db_like($form_state['values']['mail']), 'LIKE')->range(0, 1)->execute()->fetchField()
    $checkMail = user_load_by_mail($form_state['values']['mail']);
   if ($checkMail) {
       if ($GLOBALS['user']->uid) {
           form_set_error('mail', t('The e-mail address %email is already taken.', array('%email' => $form_state['values']['mail'])));
	} else {
	    form_set_error('mail', t('The e-mail address %email is already registered. <a href="@password">Have you forgotten your password?</a>', array('%email' => $form_state['values']['mail'], '@password' => url('user/password'))));
	}
    }
    
}
function cusform_user_register_form_submit($form,&$form_state){
    form_state_values_clean($form_state);
    $userinfo = array(
      'name' => $form_state['values']['name'], 
      'mail' => $form_state['values']['mail'],
      'pass' => $form_state['values']['password'], 
      'init' => $form_state['values']['mail'], 
      'status' => 1 , 
      'roles' => array(
      	DRUPAL_AUTHENTICATED_RID => TRUE,
      ),
      'field_full_name' => array( LANGUAGE_NONE => array(
                0 => array('value' => $form_state['values']['full_name'])
        )),
			
       'field_gender' => array( LANGUAGE_NONE => array(
                0 => array('value' => $form_state['values']['gender'])
        )),
    );
    $account = user_save(drupal_anonymous_user(), $userinfo);
        if(!$account){
            drupal_set_message(t("Error saving user account."), 'error');
            $form_state['redirect'] = '';
            return;
        }

        $to=$form_state['values']['mail'];
       
        $params['account']=$account;
        $params['pass']=$form_state['values']['password'];
        drupal_mail('cusform', 'register', $to, language_default(), $params);

        $_SESSION['user_id_register']=$account->uid;
        //$_SESSION['token_register_success']=generation_random_number(6);
        $_SESSION['register_success']=true;
        $form_state['redirect']='';
}
?>
